/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rhythm.commons.regx.patterns;

import com.rhythm.commons.regx.RegX;

/**
 *
 * @author Michael J. Lee
 */
public class Email{
    
    /**
     * Provides a simple email pattern E.g. someone@somewhere.com
     */
    public static final String SIMPLE_EMAIL_PATTERN = ".+@.+\\.[a-z]+";
    
    /**
     * Provides a RFC 28222 standard for email.  This standard however is not 
     * fool proof.
     */
    public static final String RFC2822_STANDARD = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+" +
            "(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|(?:[\\x01-\\x08\\x0b\\x0c\\x0e-" +
            "\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-" +
            "\\x7f])*)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:" +
            "[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)" +
            "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:" +
            "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-" +
            "\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    
    /**
     * Returns if the given <code>String</code> is a valid email.
     * @param str the source (nullable)
     * @return True is the given <code>String</code> is a valid email, otherwise
     * false
     * @see SIMPLE_EMAIL_PATTERN
     * @see RegX
     */
    public static synchronized boolean isEmail(String str){
        
        return ((str == null)? false : RegX.contains(str, RFC2822_STANDARD));
    }

    
    
}
